package io.renren.modules.photo.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.photo.dto.PhotoLabelDTO;
import io.renren.modules.photo.service.PhotoLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author huan.xu
 * @Date 2022/11/2 11:07
 * @Description
 */
@RestController
@RequestMapping("/photo/label")
@Api(tags = "照片标签管理")
public class PhotoLabelController {

  @Autowired
  private PhotoLabelService photoLabelService;

  @GetMapping("page")
  @ApiOperation("分页")
  @ApiImplicitParams({
      @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
      @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
      @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "name", value = "标签名称", paramType = "query", dataType = "String")
  })
  @RequiresPermissions("photo:label:page")
  public Result<PageData<PhotoLabelDTO>> page(
      @ApiIgnore @RequestParam Map<String, Object> params) {
    PageData<PhotoLabelDTO> page = photoLabelService.page(params);

    return new Result<PageData<PhotoLabelDTO>>().ok(page);
  }

  @GetMapping("{id}")
  @ApiOperation("信息")
  @RequiresPermissions("photo:label:info")
  public Result<PhotoLabelDTO> get(@PathVariable("id") Long id) {
    PhotoLabelDTO data = photoLabelService.getById(id);

    return new Result<PhotoLabelDTO>().ok(data);
  }

  @PostMapping
  @ApiOperation("保存")
  @LogOperation("保存")
  @RequiresPermissions("photo:label:save")
  public Result save(@RequestBody PhotoLabelDTO photoLabelDTO) {
    //效验数据
    ValidatorUtils.validateEntity(photoLabelDTO, AddGroup.class);

    Result<String> saveResult = null;
    try {
      saveResult = photoLabelService.save(photoLabelDTO);
    } catch (Exception e) {
      saveResult = new Result<String>().error(e.getMessage());
    }
    return saveResult;
  }

  @PutMapping
  @ApiOperation("修改")
  @LogOperation("修改")
  @RequiresPermissions("photo:label:update")
  public Result update(@RequestBody PhotoLabelDTO dto) {
    //效验数据
    ValidatorUtils.validateEntity(dto, UpdateGroup.class);

    Result<String> saveResult = null;
    try {
      saveResult = photoLabelService.update(dto);
    } catch (Exception e) {
      saveResult = new Result<String>().error(e.getMessage());
    }
    return saveResult;
  }

  @DeleteMapping
  @ApiOperation("删除")
  @LogOperation("删除")
  @RequiresPermissions("photo:label:delete")
  public Result delete(@RequestBody Long[] ids) {
    //效验数据
    AssertUtils.isArrayEmpty(ids, "id");

    photoLabelService.delete(ids);

    return new Result();
  }

  @GetMapping("select-options")
  @ApiOperation("组装下拉框数据")
  @RequiresPermissions("photo:label:selectOptions")
  public Result getSelectOptions(@RequestParam("query") String query) {
    List<Map> selectOptions = photoLabelService.getSelectOptions(query);
    Map returnMap = new HashMap(16);
    returnMap.put("labelSelectOptions", selectOptions);
    return new Result().ok(returnMap);
  }

  @GetMapping("download-label-photo")
  @CrossOrigin(originPatterns = "*", allowCredentials = "true", allowedHeaders = "*")
  @ApiOperation("下载标签下的所有照片")
  @RequiresPermissions("photo:label:downloadLabelPhoto")
  public void downloadLabelPhoto(@RequestParam("labelId") Long labelId,
      HttpServletRequest request, HttpServletResponse response) {
    photoLabelService.downloadLabelPhoto(labelId, request, response);
  }
}
